package com.box.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.box.entity.DbFiles;
import com.box.entity.DbFilesExample;
import com.box.entity.DbFilesExample.Criteria;
import com.box.entity.FilesSearch;
import com.box.mapper.DbFilesMapper;
import com.box.service.FilesService;
import com.box.utils.LayuiResult;
import com.box.utils.MyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class FilesServiceImpl implements FilesService {

	@Autowired
	private DbFilesMapper filesMapper;
	
	public LayuiResult selFilesList(Integer page, Integer limit, FilesSearch search) {
		// TODO Auto-generated method stub
		try {
			PageHelper.startPage(page, limit);
			DbFilesExample example =new DbFilesExample();
			example.setOrderByClause("fuploadtime desc");
			Criteria createCriteria = example.createCriteria();
			if(search.getFname()!=null&&!search.getFname().equals("")){
				createCriteria.andFnameLike("%"+search.getFname()+"%");
			}
			if(search.getFdescription()!=null&&!search.getFdescription().equals("")){
				createCriteria.andFdescriptionLike("%"+search.getFdescription()+"%");
				
			}
			if(search.getCreateTimeStart()!=null&&!search.getCreateTimeStart().equals(search.getCreateTimeStart())){
				createCriteria.andFuploadtimeGreaterThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeStart()));
			}
			if(search.getCreateTimeEnd()!=null&&!search.getCreateTimeEnd().equals(search.getCreateTimeEnd())){
				createCriteria.andFuploadtimeLessThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeEnd()));
			}
			List<DbFiles> list = filesMapper.selectByExample(example);
			PageInfo<DbFiles> pageinfo=new PageInfo<DbFiles>(list);
			LayuiResult result=new LayuiResult();
			result.setCode(0);
			result.setCount(pageinfo.getTotal());
			result.setMsg("获取文件列表成功");
			result.setData(pageinfo.getList());
			return result;
		} catch (Exception e) {
			LayuiResult result=new LayuiResult();
			result.setCode(0);
			result.setCount(null);
			result.setMsg("获取失败");
			result.setData(e);
			return result;
		}

		
	}

	public Integer delFileById(Long id) {
		// TODO Auto-generated method stub
		return filesMapper.deleteByPrimaryKey(id);
	}

	public Integer delFileByFileStr(String FilesStr) {
		// TODO Auto-generated method stub
		String[] ids = FilesStr.split(",");
		Integer n=0;
		if(ids!=null&&ids.length>0){
			for (String id : ids) {
				
			n=	filesMapper.deleteByPrimaryKey(Long.parseLong(id));
				
			}
		}
		return n;
	}

	public DbFiles selFilesById(Long id) {
		// TODO Auto-generated method stub
		return filesMapper.selectByPrimaryKey(id);
	}

	public Integer updateFilesByObject(DbFiles files) {
		// TODO Auto-generated method stub
		files.setFuploadtime(new Date());
		return filesMapper.updateByPrimaryKeySelective(files);
	}

	public Integer addFilesByObject(DbFiles files) {
		// TODO Auto-generated method stub
		files.setFuploadtime(new Date());
		return filesMapper.insertSelective(files);
	}

	

}
